package com.net.shop.car.manager.ui.personalcenter.jifen;

import java.util.ArrayList;
import java.util.List;

import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.net.shop.car.manager.BaseActivity;
import com.net.shop.car.manager.R;
import com.net.shop.car.manager.api.model.JifenGood;
import com.net.shop.car.manager.api.volley.VolleyCenter;
import com.net.shop.car.manager.api.volley.VolleyListenerImpl;
import com.net.shop.car.manager.api.volley.request.jifen.JifenExchangeList;
import com.net.shop.car.manager.api.volley.response.jifen.JifenExchangeListResponse;
import com.net.shop.car.manager.ui.view.SwitchWidget;
import com.net.shop.car.manager.ui.view.SwitchWidget.OnSwitchedListener;

import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 积分兑换列表
 * @author Deve
 *
 */
public class JifenListView implements OnSwitchedListener {

	/**
	 * 
	 */
	private SwitchWidget textSwitch;
	/**
	 * 
	 */
	private SwitchWidget lineSwitch;

	private ListView xianshangList;

	private ListView xianxiaList;

	private List<JifenGood> xianshangGoods;

	private PageIndictor pagesXianshang;

	private List<JifenGood> xianxiaGoods;

	private PageIndictor pagesXianxia;

	private BaseActivity activity;
	
	private OnJifenGoodSelectedListener onJifenGoodSelectedListener;

	public JifenListView(BaseActivity activity, View view) {
		this.activity = activity;
		textSwitch = (SwitchWidget) view
				.findViewById(R.id.jifen_list_txt_switch);
		lineSwitch = (SwitchWidget) view
				.findViewById(R.id.jifen_list_line_switch);

		xianshangList = (ListView) view.findViewById(R.id.jifen_list_xianshang);
		xianxiaList = (ListView) view.findViewById(R.id.jifen_list_xianxia);
		textSwitch.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				textSwitch.setOnSwitchedListener(JifenListView.this);
			}
		}, 20);
	}
	
	/**
	 * @author Deve
	 *
	 */
	public interface OnJifenGoodSelectedListener {
		void onJifenGoodSelected(JifenGood jifenGood,int type);
	}

	@Override
	public void onItemSelected(int index) {
		lineSwitch.setItemSelected(index);
		if (index == 0) {
			xianxiaList.setVisibility(View.GONE);
			xianshangList.setVisibility(View.VISIBLE);
			if (xianshangGoods == null) {
				xianshangGoods = new ArrayList<JifenGood>();
				pagesXianshang = new PageIndictor(1);
				getJifenGoods(pagesXianshang, 0, xianshangGoods, xianshangList);
			}
		} else if (index == 1) {
			xianshangList.setVisibility(View.GONE);
			xianxiaList.setVisibility(View.VISIBLE);
			if (xianxiaGoods == null) {
				xianxiaGoods = new ArrayList<JifenGood>();
				pagesXianxia = new PageIndictor(1);
				getJifenGoods(pagesXianxia, 1, xianxiaGoods, xianxiaList);
			}
		}
	}

	private void getJifenGoods(final PageIndictor page, final int type,
			final List<JifenGood> goods,final ListView listView) {
		VolleyCenter.getVolley().addGetRequest(
				new JifenExchangeList(type, page.pageIndex++ ),
				new VolleyListenerImpl<JifenExchangeListResponse>(
						new JifenExchangeListResponse()) {

					@Override
					public void deleveryResponse(
							JifenExchangeListResponse response) {
						goods.addAll(response.getJifenGoods());
						page.totalPages = response.getTotalPage();
						if (listView != null) {
							JifenGoodsAdapter jifenGoodsAdapter = new JifenGoodsAdapter(type, page, goods);
							listView.setAdapter(jifenGoodsAdapter);
							listView.setOnItemClickListener(jifenGoodsAdapter);
						}
					}
				});
	}
	
	/**
	 * @param txt
	 * @return
	 */
	private TextView getTextView(String txt,ViewGroup viewGroup) {
		TextView textView = (TextView) activity.inflater.inflate(R.layout.listitem_txt, viewGroup, false);
		textView.setText(txt);
		textView.setClickable(true);
		return textView;
	}

	/**
	 * @param onJifenGoodSelectedListener the onJifenGoodSelectedListener to set
	 */
	public void setOnJifenGoodSelectedListener(
			OnJifenGoodSelectedListener onJifenGoodSelectedListener) {
		this.onJifenGoodSelectedListener = onJifenGoodSelectedListener;
	}

	private class JifenGoodsAdapter extends BaseAdapter implements OnItemClickListener {

		private int type;

		private PageIndictor pageIndictor;

		private List<JifenGood> goods;

		/**
		 * @param type
		 * @param totalPages
		 * @param pageIndex
		 * @param goods
		 */
		public JifenGoodsAdapter(int type,PageIndictor pageIndictor,
				List<JifenGood> goods) {
			this.type = type;
			this.pageIndictor = pageIndictor;
			this.goods = goods;
		}

		@Override
		public int getCount() {
			if (null == goods || goods.size() == 0) {
				return 1;
			}
			if (!pageIndictor.isPageEnd()) {
				return goods.size() + 1;
			}
			return goods.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (goods == null || goods.size() == 0) {
				return getTextView("暂无数据", parent);//无数据的情况
			}
			if (!pageIndictor.isPageEnd() && position == getCount() -1) {
				TextView textView = getTextView("加载更多", parent);//当滑动到底部还有更多数据时
				textView.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						getJifenGoods(pageIndictor,type,goods,null);
					}
				});
				return textView;
			}
			View currentView = convertView;
			if (currentView == null || currentView instanceof TextView) {
				currentView = activity.inflater.inflate(R.layout.jifen_listitem, parent, false);
				currentView.setTag(new JifenListItemViewHolder(currentView));
			}
			JifenListItemViewHolder jifenListItemViewHolder = (JifenListItemViewHolder) currentView.getTag();
			jifenListItemViewHolder.setData(goods.get(position));
			return currentView;
		}

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			if (onJifenGoodSelectedListener != null) {
				onJifenGoodSelectedListener.onJifenGoodSelected(goods.get(position), type);
			}
		}
	}
	
	/**
	 * @author deve
	 *
	 */
	private class JifenListItemViewHolder { 
		
		private ImageView imageView;
		private TextView name;
		private TextView vb;
		/**
		 * 
		 */
		public JifenListItemViewHolder(View view) {
			imageView = (ImageView) view.findViewById(R.id.jifen_listitem_iv);
			name = (TextView) view.findViewById(R.id.jifen_listitem_goodname);
			vb = (TextView) view.findViewById(R.id.jifen_listitem_vb);
		}
		
		public void setData(final JifenGood good) {
			if (null == good.getGoodBitmap()) {
				VolleyCenter.getVolley().addImageRequest(good.getGoodLogo(), new Listener<Bitmap>() {

					@Override
					public void onResponse(Bitmap response) {
						good.setGoodBitmap(response);
						imageView.setImageBitmap(response);
					}
				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						
					}
				});
			} else {
				imageView.setImageBitmap(good.getGoodBitmap());
			}
			name.setText(good.getGoodName());
			vb.setText(good.getPointsRequested() + "");
		}
		
	}

	public class PageIndictor {
		public int totalPages;

		public int pageIndex;

		/**
		 * @param totalPages
		 * @param pageIndex
		 */
		public PageIndictor(int pageIndex) {
			this.pageIndex = pageIndex;
		}
		
		public boolean isPageEnd() {
			return pageIndex > totalPages;
		}
	}
}
